package com.imu.purse.config;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.springframework.util.StringUtils;

public class BitcoinConfig {
	private final static String CLASSPATH_PREFIX = "classpath:";
	private final static String PREFIX = "bitcoin";
	private final static String CLASSPATH_VALUE;
	private static ResourceBundle easemobBundle;
	
	static
	{
		CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		
		try 
		{
			easemobBundle = ResourceBundle.getBundle(PREFIX);
		}
		catch (MissingResourceException e) {}
	}
	
	private static String wrapKeyName(String key) 
	{
		if (!key.startsWith(PREFIX))
		{
			return String.format("%s.%s", PREFIX, key);
		}
		return key;
	}
	
	/**
	 * 获取bitcoin.properties文件中的key值
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		String wrapKey = wrapKeyName(key);
		return System.getProperty(wrapKey, easemobBundle.getString(wrapKey));
	}

	/**
	 * key不存在时则返回传入的默认值
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getValue(String key, String defaultValue) 
	{
		String value = defaultValue;
		
		try
		{
			value = getValue(key);
			
			if (StringUtils.isEmpty(value)) 
			{
				value = defaultValue;
			}
			
			//value = new String(value.getBytes("ISO8859-1"), "UTF8");
		} 
		catch (MissingResourceException e) {}
		catch (NullPointerException e) {} 
		
		return value;
	}

	/**
	 * 判断属性是否存在[classpath:]如果存在则拼接项目路径后返回 一般用于文件的绝对路径获取
	 * @param key
	 * @return
	 */
	public static String getClassPathValue(String key)
	{
		return getValue(key).replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE);
	}
}
